/**
 * @author : HESS MArjorie, THUILLIER Jules, G4 - CIP2
 */
package UserInterface;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;

import linkTreeInterface.Word;
import ArbreBinaire.ArbreBinaire;
import ArbreBinaire.ArbreVideException;

public class MainFrame extends JFrame implements ActionListener {
	
// D�claration des variables et objets	
	// Partie gauche de la fen�tre principale (Recherche)
	JLabel search_Label = new JLabel("Translate a word :");
	static JTextField search_Box = new JTextField("", 10);
	
	JButton translate_Button = new JButton("Translate");
	JButton add_Button = new JButton("Add");
	
	// Partie droite de la fen�tre principale (Recherche)
	JLabel translate_Label = new JLabel("Translation :");
	JLabel result_Box = new JLabel("");
	
	JButton edit_Button = new JButton("Edit");
	JButton delete_Button = new JButton("Delete");

	
	
	
public MainFrame() {
	
	 // cr�ation de l'application
    JFrame frame = new JFrame();

 // Menu 
     Menu menu = new Menu(frame);
    
	frame.setTitle("Dictionary");
	frame.setSize(450, 250);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
	frame.setLocationRelativeTo(null);

	JPanel main_Panel = (JPanel)this.getContentPane();
	  	main_Panel.setLayout(new BoxLayout(main_Panel, BoxLayout.X_AXIS));
	  	main_Panel.setBorder( new EmptyBorder(new Insets(20, 20, 20, 20)));    
	  	main_Panel.add(Box.createVerticalGlue());
	  	
	  	
	  	
// Mise en page de la partie gauche  
    JPanel left_Panel = new JPanel();
 
	    left_Panel.setLayout(new BoxLayout(left_Panel, BoxLayout.Y_AXIS));
	    left_Panel.setAlignmentX(Component.LEFT_ALIGNMENT);
	    left_Panel.add(search_Label);
	    left_Panel.add(Box.createRigidArea(new Dimension(0, 10)));
	    left_Panel.add(search_Box);	   
	    left_Panel.add(Box.createRigidArea(new Dimension(0, 20)));
	    
	    JPanel bottom_left_Panel = new JPanel();
	    	bottom_left_Panel.setLayout(new BoxLayout(bottom_left_Panel, BoxLayout.X_AXIS));
	    	bottom_left_Panel.add(translate_Button);	
	    	bottom_left_Panel.add(add_Button);
    	left_Panel.add(bottom_left_Panel);    
    	left_Panel.add(Box.createRigidArea(new Dimension(0, 80)));
    
    
// Mise en page de la partie droite   
    JPanel right_Panel = new JPanel();
	   
    	right_Panel.setLayout(new BoxLayout(right_Panel, BoxLayout.Y_AXIS));
    	right_Panel.setPreferredSize(new Dimension(300,250));
    	translate_Label.setAlignmentX(Component.LEFT_ALIGNMENT);
    	result_Box.setAlignmentX(Component.LEFT_ALIGNMENT);
	    right_Panel.add(translate_Label);
	    right_Panel.add(Box.createRigidArea(new Dimension(0, 10)));
	    
	    right_Panel.add(result_Box);	    
	    right_Panel.add(Box.createRigidArea(new Dimension(0, 10)));
	    
	    JPanel bottom_right_Panel = new JPanel();
    		bottom_right_Panel.setLayout(new BoxLayout(bottom_right_Panel, BoxLayout.X_AXIS));
    		bottom_right_Panel.add(edit_Button);	
    		bottom_right_Panel.add(delete_Button);
    	bottom_right_Panel.setAlignmentX(Component.LEFT_ALIGNMENT);
	    right_Panel.add(bottom_right_Panel); 
	    
    
// On ajoute tous les panels dans le panel principal
	left_Panel.setAlignmentX(Component.TOP_ALIGNMENT);
    main_Panel.add(left_Panel);
    main_Panel.add(Box.createRigidArea(new Dimension(40, 0)));
    right_Panel.setAlignmentX(Component.TOP_ALIGNMENT);
    main_Panel.add(right_Panel); 
    
frame.add(main_Panel);

// On ajoute des raccourcis clavier 
	//translate_button.setMnemonic('C');
    
// On ajoute les "�couteurs" pour les boutons
    translate_Button.addActionListener(this);
    add_Button.addActionListener(this);
    edit_Button.addActionListener(this);
    delete_Button.addActionListener(this);
    

    
    frame.setVisible(true);
}
	
	

@Override
public void actionPerformed(ActionEvent e) {
	
	if(e.getActionCommand() == "Translate"){
		// Si on clique sur le bouton "Translate"
		if(search_Box.getText().length()!=0){
			try {
				// Si la recherche ne donne ien, on lance une recherche regex
				String trad = UserInterface.Main.arbreWord.findTrad(UserInterface.Main.arbreBinaire, search_Box.getText());
				String tradBeta = UserInterface.Main.arbreWord.findTrad(UserInterface.Main.arbreBinaire, search_Box.getText());
				if (tradBeta==""){ 
					tradBeta =  UserInterface.Main.arbreWord.findTradRegex(UserInterface.Main.arbreBinaire, search_Box.getText()) ;
					}
				if (tradBeta == "") trad = "No translation found";
				result_Box.setText("<html>" + tradBeta + "</html>");
			} catch (ArbreVideException e1) {
				e1.printStackTrace();
			}
		}
		else {new ErrorDialog("Please enter a word to translate !");}
		
	}

	
	
	else if(e.getActionCommand() == "Add"){
		// Si on clique sur le bouton "Add"
		if(search_Box.getText().length()!=0){
			try {
				if(UserInterface.Main.arbreWord.findTrad(UserInterface.Main.arbreBinaire, search_Box.getText())==""){
				SecondFrame test = new SecondFrame("Translation", search_Box.getText(), 150);}
				else new ErrorDialog("This word is already in dictionnary\n try to Edit it");
			} catch (ArbreVideException e1) {
				e1.printStackTrace();
			}
		}
		else {new ErrorDialog("Please enter a word to add !");}
	}
	
	
	
	
	
	
	else if(e.getActionCommand() == "Edit"){
		// Si on clique sur le bouton "Edit"
		if(search_Box.getText().length()!=0){
			try {
				String traduction = UserInterface.Main.arbreWord.findTrad(UserInterface.Main.arbreBinaire,search_Box.getText());
				if (traduction.length()!=0){
				result_Box.setText(traduction);
				SecondFrame test = new SecondFrame("Edit", search_Box.getText(), 120);
				}
				else new ErrorDialog("This word is not in dictionnary !");
			} catch (ArbreVideException e1) {
				e1.printStackTrace();
			}
		}
		else {new ErrorDialog("Please enter a word to edit !");}
	}
	
	
	
	
	
	else if(e.getActionCommand() == "Delete"){
		// Si on clique sur le bouton "Delete"
		if(search_Box.getText().length()!=0){
			Word word = null;
			try {
				word = UserInterface.Main.arbreWord.findWord(UserInterface.Main.arbreBinaire, search_Box.getText());
			} catch (ArbreVideException e1) {
				e1.printStackTrace();
			}
			try {
				UserInterface.Main.arbreBinaire = UserInterface.Main.arbreWord.delete(UserInterface.Main.arbreBinaire, word);
			} catch (ArbreVideException e1) {
				e1.printStackTrace();
			}
		}
		else {new ErrorDialog("Please enter a word to delete  !");}

	}
}
}